<?php

namespace Addons\OaSystem\Controller;
use Addons\OaSystem\Controller\OaBaseController;

class OaSysLogController extends OaBaseController
{
    var $model;

    function _initialize()
    {
        $this->model = $this->getModel('oa_sys_log');
        parent::_initialize();

    }
    
    function lists(){
        if(IS_POST) {
            $querypara = json_encode($_POST);
            redirect(U('lists', array('query'=>$querypara)));
        }

        $p = empty(I('p')) ? 1 : I('p');
        $row = 20;
        $querypara = json_decode($_GET['query'], true);
        if(empty($querypara)) {
            $y_start = $y_end = date('Y');
            $m_end = date('m');
            $m_start = $m_end - 1;
            $dept_id = 0;
            $emp_name = null;
        } else {
            $y_start = $querypara['year_start'];
            $y_end = $querypara['year_end'];
            $m_start = $querypara['month_start'];
            $m_end = $querypara['month_end'];
            $dept_id = $querypara['dept_id'];
            $emp_name = $querypara['emp_name'];
        }

        for($y = 2018; $y <= 2030; $y++) {
            $query['date']['ystart'][$y]['id'] = $y;
            $query['date']['ystart'][$y]['name'] = $y;
            $query['date']['yend'][$y]['id'] = $y;
            $query['date']['yend'][$y]['name'] = $y;
        }
        for($m = 1; $m <= 12; $m++) {
            $query['date']['mstart'][$m]['id'] = $m;
            $query['date']['mstart'][$m]['name'] = $m;
            $query['date']['mend'][$m]['id'] = $m;
            $query['date']['mend'][$m]['name'] = $m;
        }
        $query['date']['ystart'][$y_start]['selected'] = 'selected';
        $query['date']['yend'][$y_end]['selected'] = 'selected';
        $query['date']['mstart'][$m_start]['selected'] = 'selected';
        $query['date']['mend'][$m_end]['selected'] = 'selected';

        $depts = $this->getDepts();
        foreach ($depts as $i => $d) {
            if($dept_id == $d['id'])
                $depts[$i]['selected'] = 'selected';
        }

        $query['depts'] = $depts;
        $query['emp_name'] = $emp_name;

        $query_start = strtotime($y_start.'-'.$m_start.'-01 00:00:00');
        $m_days = cal_days_in_month(CAL_GREGORIAN, $m_end, $y_end);
        $query_end = strtotime($y_end.'-'.$m_end.'-'.$m_days.' 23:59:59');
        $now = time();
        if($query_end > $now) $query_end = $now;
        $map['time'] = array(array('egt', $query_start), array('elt', $query_end));
        if(!empty($emp_name)) {
            if(($emp_id = $this->getEmpId($emp_name, $dept_id)) == false)
                return false;
            $map['emp_id'] = $emp_id;
        } else if(!empty($dept_id)) {
            $subdepts = db_select('oa_dept', ['pid'=>$dept_id]);
            $deptids = array_column($subdepts, 'id');
            array_push($deptids, $dept_id);
            $emp_map['dept_id'] = array('in', $deptids);
            $emps = db_select('oa_employee', $emp_map);
            $empids = array_column($emps,'id');
            $map['emp_id'] = array('in', $empids);
        }

        $len = M('oa_sys_log')->where($map)->count();
        $data = M('oa_sys_log')->where($map)->order('time desc')->page($p,$row)->select();
        foreach ($data as $i => $v) {
            $empInfo = $this->getUserInfo($v['emp_id']);
            $data[$i]['emp_name'] = $empInfo['name'];
            $data[$i]['dept'] = $empInfo['dept'];
            $data[$i]['date'] = date('Y-m-d', $v['time']);
        }

        if ($len > $row) {
            $page = new \Think\Page ( $len, $row );
            $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
            $list_data['_page'] = $page->show ();
        }
        $this->assign('post_url',addons_url('OaSystem://OaSysLog/lists', array('mdm' => I('mdm'))));
        $this->assign('_page',$list_data['_page']);
        $this->assign('list_data', $data);
        $this->assign('query', $query);
        $this->display();
    }

    private function getEmpId($emp_name, $dept_id) {
        if(!empty($dept_id)) {
            $subdepts = db_select('oa_dept', ['pid'=>$dept_id]);
            $deptids = array_column($subdepts, 'id');
            array_push($deptids, $dept_id);
            $map['dept_id'] = array('in', $deptids);
        }
        $map['name'] = $emp_name;
        if(($emp = db_find('oa_employee', $map)) == NULL) {
            $this->error('找不到该查询姓名的员工信息,请检查部门或姓名!');
            return false;
        }
        return $emp['id'];
    }

}